package leetcode_数组;

/**
 * 颜色分类
 * <p>算法: 双指针</p>
 * <P>nums[0, l - 1] = 0, nums[l, idx - 1] = 1, nums[r, n - 1] = 2 </P>
 *
 * @author yezh
 * @date 2023/7/28 14:45
 */
public class _75 {

    public void sortColors(int[] nums) {
        int l = 0, r = nums.length - 1, idx = 0;
        while (idx <= r) {
            if (nums[idx] == 0) swap(nums, l++, idx++);
            else if (nums[idx] == 1) ++idx;
            else swap(nums, idx, r--);
        }
    }

    void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

}
